#!/bin/bash
# 运行测试脚本

echo "========================================="
echo "微博热点爬虫测试套件"
echo "========================================="

# 设置Python路径
export PYTHONPATH="${PYTHONPATH}:$(pwd)/src"

# 安装依赖（如果需要）
if [ "$1" == "--install" ]; then
    echo "安装依赖..."
    pip install -r requirements.txt
fi

# 清理旧的覆盖率报告
rm -rf htmlcov .coverage

# 运行单元测试
echo ""
echo "运行单元测试..."
echo "-----------------------------------------"
pytest tests/unit/ -m "not integration" --cov-report=term

# 检查单元测试结果
if [ $? -ne 0 ]; then
    echo "❌ 单元测试失败"
    exit 1
fi

echo "✅ 单元测试通过"

# 运行集成测试
echo ""
echo "运行集成测试..."
echo "-----------------------------------------"
pytest tests/integration/ -m "not unit" --cov-append

# 检查集成测试结果
if [ $? -ne 0 ]; then
    echo "❌ 集成测试失败"
    exit 1
fi

echo "✅ 集成测试通过"

# 生成覆盖率报告
echo ""
echo "生成覆盖率报告..."
echo "-----------------------------------------"
pytest --cov=src --cov-report=html --cov-report=term-missing

# 显示总结
echo ""
echo "========================================="
echo "测试完成！"
echo "========================================="
echo "✅ 所有测试通过"
echo ""
echo "覆盖率报告已生成："
echo "- 终端报告：见上方输出"
echo "- HTML报告：htmlcov/index.html"
echo ""

# 检查覆盖率是否达标
coverage report --fail-under=80
if [ $? -ne 0 ]; then
    echo "⚠️  警告：代码覆盖率低于80%"
    exit 1
fi

echo "✅ 代码覆盖率达标（>=80%）"